package com.example.demo.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.example.demo.ao.BulkAo;
import com.example.demo.mapper.BulkInfoMapper;
import com.example.demo.service.BulkInfoService;
import com.example.demo.vo.BulkUserVo;
import com.example.demo.vo.BulkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BulkInfoServiceImpl implements BulkInfoService {


    @Autowired
    private BulkInfoMapper bulkInfoMapper;

    @Override
    public Map<String, List<BulkVo>> getBulkInfoList(BulkAo bulkAo) {
        List<BulkVo> bulkInfoList = bulkInfoMapper.getBulkInfoList(bulkAo);

        //合计
        BulkVo total = BulkVo.setValue();
        total.setType("合计");

        //直接交易合计
        BulkVo totalDirect = BulkVo.setValue();
        totalDirect.setType("直接交易合计");

        //不含直接交易合计
        BulkVo totalDirectNot = BulkVo.setValue();
        totalDirectNot.setType("不含直接交易合计");

        List<BulkVo> bulkInfoListSecond = new ArrayList<>();
        Map<String, List<BulkVo>> collect = bulkInfoList.stream().collect(Collectors.groupingBy(item -> item.getMgtOrgCode()
                + item.getMgtOrgName() + item.getPrcCode()));
        collect.forEach((type, list)->{
            BulkVo info = BulkVo.setValue();
            for (BulkVo bulkVo : list) {
                if (bulkVo.getAttrCls().startsWith("02") || bulkVo.getAttrCls().startsWith("03")) {
                    info.setType("直接交易明细");
                }
                if (!bulkVo.getAttrCls().startsWith("02") && !bulkVo.getAttrCls().startsWith("03")) {
                    info.setType("不含直接交易明细");
                }
                info.setMgtOrgCode(bulkVo.getMgtOrgCode());
                info.setMgtOrgName(bulkVo.getMgtOrgName());
                info.setCustNo(bulkVo.getCustNo());
                info.setPrcCode(bulkVo.getPrcCode());
                info.setSettleQty(bulkVo.getSettleQty().add(info.getSettleQty()));
                info.setSettleExp(bulkVo.getSettleExp().add(info.getSettleExp()));
            }
            bulkInfoListSecond.add(info);
        });

        for (BulkVo bulkVo : bulkInfoList) {
            total.setSettleQty(bulkVo.getSettleQty().add(total.getSettleQty()));
            total.setSettleExp(bulkVo.getSettleExp().add(total.getSettleExp()));

            if (bulkVo.getAttrCls().startsWith("02") || bulkVo.getAttrCls().startsWith("03")) {
                totalDirect.setSettleQty(bulkVo.getSettleQty().add(totalDirect.getSettleQty()));
                totalDirect.setSettleExp(bulkVo.getSettleExp().add(totalDirect.getSettleExp()));
            }

            if (!bulkVo.getAttrCls().startsWith("02") && !bulkVo.getAttrCls().startsWith("03")) {
                totalDirectNot.setSettleQty(bulkVo.getSettleQty().add(totalDirectNot.getSettleQty()));
                totalDirectNot.setSettleExp(bulkVo.getSettleExp().add(totalDirectNot.getSettleExp()));
            }
        }

        bulkInfoListSecond.add(total);
        bulkInfoListSecond.add(totalDirect);
        bulkInfoListSecond.add(totalDirectNot);
        Map<String, List<BulkVo>> map = bulkInfoListSecond.stream().collect(Collectors.groupingBy(BulkVo::getType));
        return map;
    }



    @Transient
    @Override
    public int getBulkUserList(List<BulkAo> bulkAo) {
        List<BulkUserVo> objects = new ArrayList<>();
        int num = 0;
        List<BulkUserVo> bulkUserList = bulkInfoMapper.getBulkUserList();
        for (BulkAo ao : bulkAo) {
            List<BulkVo> vo = bulkInfoMapper.getBulkInfo(ao);
//            System.out.println(vo);
//            objects.addAll(bulkUserList);
//            System.out.println(objects);
            for (BulkVo bulkVo : vo) {
                for (BulkUserVo bulkUserVo : bulkUserList) {
                    if (CollectionUtil.isEmpty(bulkUserList) || !bulkVo.getCustNo().equals(bulkUserVo.getCustNo())) {
                        num = bulkInfoMapper.insertBulkInfo(ao);
                    }else {
                        num = bulkInfoMapper.updateBulkInfo(ao);
                    }
                }

            }


        }


        return num;
    }












    public  void main(String[] args) {
        BulkAo bulkAo = new BulkAo();
        bulkAo.setMgtOrgCode("65401");
        bulkAo.setQtyChargYm("202401");
        List<BulkVo> bulkInfoList = bulkInfoMapper.getBulkInfoList(bulkAo);
        BulkVo total = BulkVo.setValue();
        for (BulkVo bulkVo : bulkInfoList) {
            if (bulkVo.getType().equals(total.getType())){
                Map<String, List<BulkVo>> map = bulkInfoList.stream().collect(Collectors.groupingBy(BulkVo::getAttrCls));
                map.forEach((key, value) -> {
                });
            }
        }

    }
}
